#include "main.h"

#define DISPLAY_MAX 2
#define SETTINGS_MAX 3

unsigned char displayState = 1;
unsigned char settingsState = 0;
volatile unsigned char button1Pressed = 0;
volatile unsigned char button2Pressed = 0;

// *******************************************************************
// **** Struktur fuer LCD Ausgabe ************************************
// *******************************************************************
unsigned char layout;	

const struct LCD_LAYOUT 
{
	char			xpos;
	char			ypos;
	char 			displayPlusSign;
	char 			numAllDigits;
	char 			decDigits;
	char 			dimStr[8];
	char 			dimStrLen;
	char 			lcdStrLen;
} LAYD[2][4] =
{	
	{	// ****  Daten fuer Layout 1 (Fahren)  ******************	
		{  			// Sturktur fuer Geschwindigkeit
			0,		// xpos
			0,		// ypos
			0,   	// displayPlusSign
			4,		// numAllDigits
			0,		// decDigits
			" rpm",	// dimStr[8]
			4,		// dimStrLen
			8		// lcdStrLen
		},
		{  			// Sturktur fuer UBat
			8,		// xpos
			0,		// ypos
			0,   	// displayPlusSign
			3,		// numAllDigits
			1,		// decDigits
			" V",	// dimStr[8]
			2,		// dimStrLen
			8		// lcdStrLen
		},
		{  			// Sturktur fuer IBat
			0,		// xpos
			1,		// ypos
			0,   	// displayPlusSign
			3,		// numAllDigits
			1,		// decDigits
			" A",	// dimStr[8]
			2,		// dimStrLen
			8		// lcdStrLen
		},
		{  			// Sturktur fuer Temp
			8,		// xpos
			1,		// ypos
			0,   	// displayPlusSign
			4,		// numAllDigits
			0,		// decDigits
			" C",	// dimStr[8]
			2,		// dimStrLen
			8		// lcdStrLen
		}
	},
	{	// ****  Daten fuer Layout 2 (Laden)  ******************	
		{  			// Sturktur position 1 in layout 2
			0,		// xpos
			0,		// ypos
			0,   	// displayPlusSign
			3,		// numAllDigits
			1,		// decDigits
			" V",	// dimStr[8]
			2,		// dimStrLen
			8		// lcdStrLen
		},
		{  			// Sturktur position 2 in layout 2
			8,		// xpos
			0,		// ypos
			0,   	// displayPlusSign
			4,		// numAllDigits
			0,		// decDigits
			" mV",	// dimStr[8]
			3,		// dimStrLen
			8		// lcdStrLen
		},
		{  			// Sturktur position 3 in layout 2
			0,		// xpos
			1,		// ypos
			0,   	// displayPlusSign
			4,		// numAllDigits
			0,		// decDigits
			" mV",	// dimStr[8]
			3,		// dimStrLen
			8		// lcdStrLen
		},
		{  			// Sturktur position 4 in layout 2
			8,		// xpos
			1,		// ypos
			0,   	// displayPlusSign
			4,		// numAllDigits
			0,		// decDigits
			" d",	// dimStr[8]
			2,		// dimStrLen
			8		// lcdStrLen
		}
	}
};





SIGNAL(INT0_vect){
	button1Pressed = TRUE;
}

SIGNAL(INT1_vect){
	//delay_ms(1);
	//if ((BUTTON_PIN & (1<<BUTTON2_PIN)) == 0)
		button2Pressed = TRUE;
}



//========================= main ==============================

int main (void){

	
	//Enable Interrupt for buttons
	EICRA = 	(1<<ISC00)|(1<<ISC01); //falling edge on INT0 and INT1 generates interrupt
	EIMSK = 	(1<<INT0)|(1<<INT1); 

	//Configure Buttons as input with pullup
	BUTTON_DDR &= ~((1<<BUTTON1_PIN) | (1<<BUTTON2_PIN));
	BUTTON_PORT |= ((1<<BUTTON1_PIN) | (1<<BUTTON2_PIN));
	
	//enable display
	LCD_PWR_DDR|= (1<<LCD_PWR_PIN);
	LCD_PWR_PORT|= (1<<LCD_PWR_PIN);
	//enable backlight
	LCD_BL_DDR|= (1<<LCD_BL_PIN);
	LCD_BL_PORT|= (1<<LCD_BL_PIN);
	
	lcd_init(LCD_DISP_ON);	
	lcd_clrscr();
	lcd_puts("LowRider");
	
	//wait for signals to settle
	delay_ms(1000);
		
	//SPI slave
	spiSlaveInit();       
	
	sei();
	delay_ms(1);
	button1Pressed = FALSE;
	button2Pressed = FALSE;
	

	while(1)
	{
		signed int i;
		//wait for buttons or SPI data (we return from sleep mode) -> delay necessary
		delay_ms(5);
		
		lcd_clrscr();
		
		//debug
		//rxBuffer[2] = (0x01 & button1Pressed) | (0x02 & button2Pressed);
		
		
		// lcd_put_phys(unsigned char pos_sign_disp; signed int phy_val, unsigned char stellen, unsigned char nach_komma, unsigned char *dim_str)
		
		// putNormed2LCD(	signed int PhysVal, unsigned char displayPlusSign, 
		//		unsigned char numAllDigits, unsigned char decDigits,
		//		char *dimStr, char dimStrLen, char lcdStrLen);
		
		layout = (char)rxBuffer[0];  // selection of layout
		if (layout == 0xFF)
		{
			//character mode
			for (i=1;i<17;i++)
				lcd_putc(rxBuffer[i]);
			lcd_gotoxy(0,1);
			
			for (i=17;i<33;i++)
				lcd_putc(rxBuffer[i]);
		}
		else
		{
			
			// *** write data for position 1 ************** 
			i = rxBuffer[2] | (rxBuffer[3] << 8);
			lcd_gotoxy(LAYD[layout][0].xpos,LAYD[layout][0].ypos);
			putNormed2LCD(	i,
							LAYD[layout][0].displayPlusSign,
							LAYD[layout][0].numAllDigits,
							LAYD[layout][0].decDigits,
							&LAYD[layout][0].dimStr,
							LAYD[layout][0].dimStrLen,
							LAYD[layout][0].lcdStrLen);
			
			// *** write data for position 2 ************** 
			i = rxBuffer[4] | (rxBuffer[5] << 8);
			lcd_gotoxy(LAYD[layout][1].xpos,LAYD[layout][1].ypos);
			putNormed2LCD(	i,
							LAYD[layout][1].displayPlusSign,
							LAYD[layout][1].numAllDigits,
							LAYD[layout][1].decDigits,
							&LAYD[layout][1].dimStr,
							LAYD[layout][1].dimStrLen,
							LAYD[layout][1].lcdStrLen);
			
			// *** write data for position 3 ************** 
			i = rxBuffer[6] | (rxBuffer[7] << 8);
			lcd_gotoxy(LAYD[layout][2].xpos,LAYD[layout][2].ypos);
			putNormed2LCD(	i,
							LAYD[layout][2].displayPlusSign,
							LAYD[layout][2].numAllDigits,
							LAYD[layout][2].decDigits,
							&LAYD[layout][2].dimStr,
							LAYD[layout][2].dimStrLen,
							LAYD[layout][2].lcdStrLen);
			
			// *** write data for position 4 ************** 
			i = rxBuffer[8] | (rxBuffer[9] << 8);
			lcd_gotoxy(LAYD[layout][3].xpos,LAYD[layout][3].ypos);
			putNormed2LCD(	i,
							LAYD[layout][3].displayPlusSign,
							LAYD[layout][3].numAllDigits,
							LAYD[layout][3].decDigits,
							&LAYD[layout][3].dimStr,
							LAYD[layout][3].dimStrLen,
							LAYD[layout][3].lcdStrLen);
		}
		
		//Buttons
		//save button state in txBuffer
		txBuffer[0] = (0x01 & button1Pressed) | (0x02 & button2Pressed);
		
		if (button1Pressed){
			//lcd_clrscr();
			//lcd_puts("Hello ");
			//lcd_puti(txBuffer[0],1);
			button1Pressed = FALSE;			
		}
		else if (button2Pressed){
			//lcd_clrscr();
			//lcd_puts("Dude ");
			//lcd_puti(txBuffer[0],1);
			button2Pressed = FALSE;
		}
		
		
		
		set_sleep_mode(SLEEP_MODE_IDLE);
        sleep_mode();                   // goto sleep (4mA without BL (Display on), 11mA with BL)	
	}	
	
	
	
}


